#!/usr/bin/perl
use warnings;
use strict;
use Benchmark qw(cmpthese timethese);
# sub double_star {
  # my $string = shift;
  # $string =~ s/^\s*//;
  # $string =~ s/\s*$//;
  # return $string;
# }
# sub double_plus {
  # my $string = shift;
  # $string =~ s/^\s+//;
  # $string =~ s/\s+$//;
  # return $string;
# }
# sub replace {
  # my $string = shift;
  # $string =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
  # return $string;
# }
# sub for_star {
  # my $string = shift;
  # for ($string) { s/^\s+//; s/\s+$//; }
  # return $string;
# }
# sub for_plus {
  # my $string = shift;
  # for ($string) { s/^\s*//; s/\s*$//; }
  # return $string;
# }
sub regex_or {
  my $string = shift;
  $string =~ s/(?:^ +)||(?: +$)//g;
  return $string;
}
# cmpthese(
  # -1,
  # {
    # 'double_star' => q|double_star('    Mary had a little lamb.   ');|,
    # 'double_plus' => q|double_plus('    Mary had a little lamb.   ');|,
    # 'replace'     => q|replace(    '    Mary had a little lamb.   ');|,
    # 'for_star'    => q|for_star(   '    Mary had a little lamb.   ');|,
    # 'for_plus'    => q|for_plus(   '    Mary had a little lamb.   ');|,
    # 'regex_or'    => q|regex_or(   '    Mary had a little lamb.   ');|,
  # }
# );
my $val = regex_or("   test   saff    + sdfsd sdfsd as            ");
print $val;